#!/usr/bin/env ruby

from_email, to_email = *ARGV

abort "Usage: script/change_email [FROM_EMAIL] [TO_EMAIL]" if from_email.nil? || to_email.nil?

ENV["RAILS_ENV"] ||= "production"
require_relative "../config/environment"

from_user = User.find_by_email!(from_email)
from_user.email = to_email
from_user.email_confirmed = false
from_user.generate_confirmation_token

if from_user.save
  puts "#{from_user.name}'s email is now: #{to_email}"
  Mailer.email_confirmation(from_user).deliver_later
  puts "enqueued email confirmation mail."
else
  puts "could not save user"
  puts from_user.errors.full_messages.to_sentence
end
